package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.FoodDao;
import dao.FoodTypeDao;
import dao.RateDao;
import dao.RowMapper;
import entity.FoodType;

public class FoodTypeDaoImpl implements FoodTypeDao{
	JDBCTemplate jdbc =new JDBCTemplate();
	FoodDao fd =new FoodDaoImpl();
	RateDao rd =new RateDaoImpl();
	
	@Override
	public List<FoodType> queryAllFoodType() {
		String sql="select * from FoodType";
		return jdbc.queryAll(sql, new rowMapperImpl());
	}

	@Override
	public FoodType queryFoodTypeByFoodTypeId(int foodTypeId) {
		String sql ="select * from foodtype where ftid =?";
		return jdbc.queryOne(sql, new rowMapperImpl(), foodTypeId);
	}
	
	private class rowMapperImpl implements RowMapper<FoodType>{
		@Override
		public FoodType rowMapper(ResultSet rs) throws SQLException {
			//(ftId, foodTypeName, foodList, rateList)
			return new FoodType(rs.getInt("ftId"),
					rs.getString("foodTypeName"),null,null);
		}
		
	}

	@Override
	public void addFoodType(FoodType foodtype) {
		String sql="INSERT INTO foodtype(foodTypeName) VALUES(?)";
		jdbc.CUDObject(sql, foodtype.getFoodTypeName());
	}

	@Override
	public void deleteFoodType(int ftid) {
		String sql ="delete from foodtype WHERE ftid=?";
		jdbc.CUDObject(sql, ftid);
		
	}

	@Override
	public void updateFoodType(FoodType foodtype) {
		String sql ="UPDATE foodtype SET foodTypeName=? WHERE ftid=?";
		jdbc.CUDObject(sql, foodtype.getFoodTypeName(),foodtype.getFtId());
	}
}
